<?php
/*  ================================================================== *\
    (C) Copyright 2009 by Kenneth Downs, Donald Organ, Justin Dearing

    This file is part of Triangulum.

\*  ================================================================== */

/**
 * Debug Test Suite Execution Program.
 * 
 * This is the comprehensive test management program for all
 * debugging tests.  Debugging tests ensure that spec files
 * are processed correctly.  
 * 
 * Run-time tests, which ensure that the built database 
 * functions correctly, are handled by {@link tests-runtime.php}
 * 
 * This program expects the "tests" directory to be
 * in a subdirectory just below this actual program. 
 * 
 * Each test has a file in it called "desc.txt" that gives a short
 * description of the test.
 * 
 * To devise test, follow these steps:
 * - Pick one extremely precise error to trap or condition to test.
 * - Make a directory in tests.
 * - Put a file "desc.txt" with one short line describing the test.
 * - Make a dd.yaml file with the same name as the directory,
 *   includes are fine.
 * - Run the builder.
 * - Examine the "debug" files.  Pick one or more files that 
 *   correctly describe what is being tested, and copy them to
 *   a "compare" subdirectory for the test.  These are the files
 *   that must match in future runs for the test to pass.
 * 
 * The debug output consists of multiple files, each containing the
 * output of print_r() for a particular associative array. 
 * 
 * The current test series are defined as:
 * 
 *   Generally LOAD and Merge
 * 
 * - 000.0--  Error checking at file load time.  Pretty basic 
 *            because we are not looking for interactions between
 *            features, just the very basics.
 * - 001.0--  Merge testing.  In-depth repeats of basic tests as
 *            they apply to multiple files. 
 * - 001.1--  Merge testing comprehensive
 * 
 *   Assign Classes and Reformat
 * 
 * - 002.0--  Property and class assignment, simple
 * - 002.1--  Properties, invalid properties
 * - 002.2--  Properties, missing properties
 *
 *   Hardcoded Validations
 * - 003.0--  Miscellaneous hardcoded validations
 * 
 *   Primary key and foreign key reformats
 *   
 * - 004.0--  Sequencing Tests
 * - 004.1--  Primary Key tests
 * - 004.2--  Foreign key tests
 *
 * @package Andromeda
 * @author Ken Downs <ken@secdat.com>
 */

main();

/**
 * 
 * @return unknown_type
 */
function main() {
    $dirs = scandir("./tests");
    $prefix = '000.0';
    $pass   = 0;
    $failed = 0;
    $notest = 0;
    foreach($dirs as $idx=>$dir) {
        if(substr($dir,0,1)=='.') continue;
        
        if(substr($dir,0,5)<>$prefix) {
            echo "\n\n";
        }
        $prefix = substr($dir,0,5);
        
        # Run the test
        `php triBuild.php tests/$dir`;
        
        # Get the description
        $desc = file_get_contents("tests/$dir/desc.txt");
        while(substr($desc,-1)=="\n") {
            $desc = substr($desc,0,strlen($desc)-1);
        }
        
        # For making things line up
        $xdir = str_pad($dir,25);

        # Now read the files we are supposed to check, get
        # rid of '.' and '..'
        if(!is_dir("tests/$dir/compare")) {
            echo "\n----   $xdir $desc";
            echo "\n  --   the compare/ subdirectory does not exist";
            $notest++;
            continue;
        }
        $files = scandir("tests/$dir/compare");
        if(!is_array($files)) {
            echo "\n----   $xdir $desc";
            echo "\n  --   there are no files in the compare/ subdirectory";
            $notest++;
            continue;
        }
        array_shift($files);
        array_shift($files);
        foreach($files as $idx=>$file) {
            if(substr($file,-1)=='~') unset($files[$idx]);
        }
        if(count($files)==0) {
            echo "\n----   $xdir $desc";
            echo "\n  --   there are no files in the compare/ subdirectory";
            $notest++;
            continue;
        }
        
        # Initialize an empty list of failures and do the comparisons
        $fail = array();
        foreach($files as $file) {
            # KFD 5/14/09 general cleanup
            if(substr($file,0,1)=='.') continue;
            $compare = file_get_contents("tests/$dir/compare/$file");
            $debug   = file_get_contents("tests/$dir/debug/$file");
            
            if($compare<>$debug) {
                $fail[] = $file;
            }
        }
        
        if(count($fail)==0) {
            $pass++;
            echo "\nPASS   $xdir $desc";
        }
        else {
            $failed++;
            echo "\nFAIL** $xdir $desc\n";
            echo "\n  Files that failed:";
            foreach($fail as $onefail) {
                echo "\n    - $onefail";
            }
            echo "\n";
        }
    } 
    echo "\n\n";
    $pf = intval($pass) + intval($failed) + intval($notest);
    echo "\nPassed : ".str_pad(number_format($pass   ,0),6,' ',STR_PAD_LEFT);
    echo "\nNo Test: ".str_pad(number_format($notest ,0),6,' ',STR_PAD_LEFT);
    echo "\nFailed : ".str_pad(number_format($failed ,0),6,' ',STR_PAD_LEFT);
    echo "\nTOTAL  : ".str_pad(number_format($pf     ,0),6,' ',STR_PAD_LEFT);
    echo "\n\n";
}

?>
